const {
    comMysql
} = require("../db/mysql")

const addHistory = (req, res) => {
    let {
        username,
        blogId,
        blogName
    } = req.body
    let time = new Date().getTime()
    let sql = `insert into history(username,closeTime,blogId,blogName) values (?,?,?,?)`
    let arr = [username, time, blogId, blogName]
    return comMysql(sql, arr)
}
const deleteHistory = (req, res) => {
    let {
        username,
        blogId
    } = req.body
    let sql = `delete from history where blogId = ? and username = ?`
    let arr = [blogId, username]
    return comMysql(sql, arr)
}
const searchHistory = (req, res) => {
    let {
        username,
        blogId
    } = req.query
    let sql = ""
    let arr
    if (blogId) {
        sql = `select * from history where username = ? and blogId = ?`
        arr = [username, blogId]
    } else {
        sql = `select * from history where username = ?`
        arr = [username]
    }
    return comMysql(sql, arr)
}
const updateTime = (req, res) => {
    let {
        id
    } = req.body
    let sql = `select * from history where id = ?`
    let arr = [id]
    let data = comMysql(sql, arr)
    if (data.length > 0) {
        let sql1 = `update history set time = ${new Date().getTime()} where historyId = ?`
        let arr1 = [id]
        return comMysql(sql1, arr1)
    } else {
        return null
    }
}
module.exports = {
    addHistory,
    deleteHistory,
    searchHistory,
    updateTime
}